﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class CommandApiController : ApiController
    {
        public CommandRepository Get(int? pageNumber, string orderBy, string orderDirection)
        {
            try
            {
                string whereClause = "IsDeleted != true";

                return new CommandRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public Command Get(int? id)
        {
            try
            {
                var command = new Command();
                var commanRepository = new CommandRepository();
                command.CommandCode = GenerateCode.GetCode(commanRepository.GetNextIndex(), CodeType.Order);
                if (!(!id.HasValue || id == 0))
                {
                    command = commanRepository.GetByID(id.Value);
                }
                return command;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var commandRepository = new CommandRepository();
                    commandRepository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public CommandRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var commandRepository = new CommandRepository();
                commandRepository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public Command Put(Command command)
        {
            try
            {
                var commandRepository = new CommandRepository();
                int commandID = commandRepository.Update(command);

                return Get(commandID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }
    }
}
